package com.example.shiwu.adapter;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.shiwu.ItemInfoDetailsActivity;
import com.example.shiwu.ItemInfoEditActivity;
import com.example.shiwu.ItemInfoListActivity;
import com.example.shiwu.R;
import com.example.shiwu.db.model.Items;
import com.example.shiwu.db.model.Sends;
import com.example.shiwu.service.ItemsService;
import com.example.shiwu.service.SendsService;
import com.example.shiwu.util.FileDownUtil;

import java.util.ArrayList;
import java.util.List;

public class MySimpleAdapter extends BaseAdapter {

    private List<Items> listItems = new ArrayList<Items>();
    private int resourceId;

    private ItemInfoListActivity mContext;
    private String itemId;
    private ItemsService itemsService;
    private SendsService sendsService;

    public MySimpleAdapter(ItemInfoListActivity mContext, List<Items> listItems, int resource) {
        this.resourceId = resource;
        this.mContext = mContext;
        this.listItems = listItems;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View view;
        final ViewHolder viewHolder;

        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder(view, position);
            view.setTag(viewHolder);
            itemsService = new ItemsService();
            sendsService = new SendsService();
            view.findViewById(R.id.items_detail).setTag(position);
            viewHolder.itemsName.setText(listItems.get(position).getName());
            viewHolder.itemsLocaltion.setText(listItems.get(position).getLocation());
            viewHolder.itemsNum.setText(listItems.get(position).getNum());
            viewHolder.itemsDeviceType.setText(listItems.get(position).getDeviceType());
            if(listItems.get(position).getStatus() == 0){
                viewHolder.items_status.setVisibility(View.INVISIBLE);
            }


        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.itemsName.setText(listItems.get(position).getName());
        viewHolder.itemsLocaltion.setText(listItems.get(position).getLocation());
        viewHolder.itemsNum.setText(listItems.get(position).getNum());
        viewHolder.itemsDeviceType.setText(listItems.get(position).getDeviceType());
        if(listItems.get(position).getStatus() == 0){
            viewHolder.items_status.setVisibility(View.INVISIBLE);
        }

        //按钮点击事件
        viewHolder.detailsSub.setOnClickListener(new View.OnClickListener() { //详情
            @Override
            public void onClick(View v) {

                //Toast.makeText(MyContext, "id === " + v.getId(), Toast.LENGTH_SHORT).show();
                //得到view在父容器中的位置下标
                int position = (Integer) v.getTag();
                itemId = String.valueOf(listItems.get(position).getId());
                System.out.println("id =================================== " + itemId);
                Intent intent = new Intent(v.getContext(), ItemInfoDetailsActivity.class);
                intent.putExtra("itemId",itemId);
                v.getContext().startActivity(intent);
            }
        });
        viewHolder.editSub.setOnClickListener(new View.OnClickListener() {//编辑
            @Override
            public void onClick(View v) {
                //Toast.makeText(MyContext, "id === " + v.getId(), Toast.LENGTH_SHORT).show();
                System.out.println("id =================================== " + v.getId());
                itemId = String.valueOf(listItems.get(position).getId());
                System.out.println("id =================================== " + itemId);
                Intent intent = new Intent(v.getContext(), ItemInfoEditActivity.class);
                intent.putExtra("itemId",itemId);
                v.getContext().startActivity(intent);
            }
        });
        viewHolder.delSub.setOnClickListener(new View.OnClickListener() {//删除
            @Override
            public void onClick(View v) {
                System.out.println("id =================================== " + v.getId());
                //点击删除按钮之后，给出dialog提示
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle("确认删除?");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.out.println("id =================================== " + position);
                        int itemId = listItems.get(position).getId();
                        Items items = new Items(listItems.get(position).getId());
                        List<Sends> lisSend = sendsService.getSends(new Sends(itemId));

                        if(lisSend.size()>0){
                            String uid = sendsService.getSends(new Sends(itemId)).get(0).getUid();
                            int size = itemsService.deleteAll(items);
                            if(size >0){
                                String fileName = String.valueOf(itemId) + "-" + uid + ".cvs";
                                Boolean bool1 = FileDownUtil.deleteSingleFile(FileDownUtil.DOWN_NAME + fileName);
                                if (bool1) {
                                    Toast.makeText(v.getContext(), "删除成功！", Toast.LENGTH_SHORT).show();
                                    mContext.updataData();
                                    //Intent intent = new Intent(mContext, ItemInfoActivity.class);
                                   //mContext.startActivity(intent);
                                }
                            }
                        }else {
                            int size = itemsService.delete(items);
                            if(size >0){
                                listItems.remove(getItem(position));
                                //if(bool2){
                                    //notifyDataSetChanged();
                                mContext.updataData();
                                    Toast.makeText(v.getContext(), "删除成功！", Toast.LENGTH_SHORT).show();
                               // }
                            }
                        }

                    }
                });
                builder.show();
            }
        });
        return view;
    }

    public void cleckAll() {
        notifyDataSetChanged();
    }

    class ViewHolder {

        TextView editSub,delSub,detailsSub,itemsName,itemsLocaltion,itemsNum,itemsDeviceType,items_status;

        public ViewHolder(View view, int position) {
            editSub = view.findViewById(R.id.items_edit_sb);
            delSub = view.findViewById(R.id.items_del_sb);
            detailsSub = view.findViewById(R.id.items_detail);

            itemsName =view.findViewById(R.id.items_name);
            itemsLocaltion = view.findViewById(R.id.items_localtion_val);
            itemsNum = view.findViewById(R.id.items_num_val);
            itemsDeviceType = view.findViewById(R.id.items_device_type_val);
            items_status = view.findViewById(R.id.items_status);
        }

    }

    //    完成按钮
    public interface OnItemCompletedListener {
        void onCompletedClick(int position);
    }

    private OnItemCompletedListener onItemCompletedListener;

    public void setOnItemCompletedListener(OnItemCompletedListener onItemCompletedListener) {
        this.onItemCompletedListener = onItemCompletedListener;
    }

    //    取消完成
    public interface OnItemCancelCompletedListener {
        void onCancelCompletedClick(int position);
    }

    private OnItemCancelCompletedListener onItemCancelCompletedListener;

    public void setOnItemCancelCompletedListener(OnItemCancelCompletedListener onItemCancelCompletedListener) {
        this.onItemCancelCompletedListener = onItemCancelCompletedListener;
    }

}
